//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.baosight.iplat4j.core.data.ibatis.data.dao;

import com.baosight.iplat4j.core.data.ibatis.SqlMapClientFactoryBean;
import com.baosight.iplat4j.core.data.ibatis.SqlMapClientTemplate;
import com.baosight.iplat4j.core.data.ibatis.dao.BatchDao;
import com.baosight.iplat4j.core.data.ibatis.dao.Dao;
import com.baosight.iplat4j.core.data.ibatis.sql.client.SqlMapClient;
import com.baosight.iplat4j.core.data.ibatis.sql.event.SqlMapDeleteListener;
import com.baosight.iplat4j.core.data.ibatis.sql.event.SqlMapDeleteMonitor;
import com.baosight.iplat4j.core.data.ibatis.sql.event.SqlMapInsertListener;
import com.baosight.iplat4j.core.data.ibatis.sql.event.SqlMapInsertMonitor;
import com.baosight.iplat4j.core.data.ibatis.sql.event.SqlMapUpdateListener;
import com.baosight.iplat4j.core.data.ibatis.sql.event.SqlMapUpdateMonitor;
import com.baosight.iplat4j.core.data.ibatis.support.SqlMapClientDaoSupport;
import com.baosight.iplat4j.core.data.sql.SQLEnhancer;
import com.baosight.iplat4j.core.data.sql.SQLEnhancerManager;
import com.baosight.iplat4j.core.exception.PlatException;
import com.baosight.iplat4j.core.security.dao.DaoSecurityManager;
import com.baosight.iplat4j.core.service.soa.DomainQuery;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.security.access.AccessDeniedException;

public class SqlMapDao extends SqlMapClientDaoSupport implements Dao, BatchDao, BeanNameAware {
    public static int MAX_QUERY_COUNT = 1000;
    private int maxQueryCount;
    protected String name;
    protected boolean autoCount;
    protected boolean autoLimit;
    protected String orderKey;
    protected List<SqlMapInsertMonitor> insertMonitors;
    protected List<SqlMapInsertListener> insertListeners;
    protected List<SqlMapUpdateMonitor> updateMonitors;
    protected List<SqlMapUpdateListener> updateListeners;
    protected List<SqlMapDeleteMonitor> deleteMonitors;
    protected List<SqlMapDeleteListener> deleteListeners;
    protected SQLEnhancer sqlEnhancer;

    public SqlMapDao() {
        this.maxQueryCount = MAX_QUERY_COUNT;
        this.sqlEnhancer = null;
        this.setSqlMapClientTemplate(new SqlMapClientTemplate());
    }

    public void setInsertMonitors(List<SqlMapInsertMonitor> insertMonitors) {
        this.insertMonitors = insertMonitors;
    }

    public void setInsertListeners(List<SqlMapInsertListener> insertListeners) {
        this.insertListeners = insertListeners;
    }

    public void setUpdateMonitors(List<SqlMapUpdateMonitor> updateMonitors) {
        this.updateMonitors = updateMonitors;
    }

    public void setUpdateListeners(List<SqlMapUpdateListener> updateListeners) {
        this.updateListeners = updateListeners;
    }

    public void setDeleteMonitors(List<SqlMapDeleteMonitor> deleteMonitors) {
        this.deleteMonitors = deleteMonitors;
    }

    public void setDeleteListeners(List<SqlMapDeleteListener> deleteListeners) {
        this.deleteListeners = deleteListeners;
    }

    public SQLEnhancer getSqlEnhancer() {
        return this.sqlEnhancer;
    }

    public void setSqlEnhancer(SQLEnhancer sqlEnhancer) {
        this.sqlEnhancer = sqlEnhancer;
    }

    public void setMaxQueryCount(int maxQueryCount) {
        this.maxQueryCount = maxQueryCount < MAX_QUERY_COUNT ? MAX_QUERY_COUNT : maxQueryCount;
    }

    public List query(Object parameters) {
        return this.query("", parameters);
    }

    public List query(String name, Object parameters) {
        SQLEnhancerManager.setSqlEnhancerThreadLocal(this.sqlEnhancer);
        String statementName = this.getStatementName("query", name);
        return this.getSqlMapClientTemplate().queryForList(statementName, parameters, 0, this.maxQueryCount);
    }

    public List queryAll(String name, Object parameters) {
        SQLEnhancerManager.setSqlEnhancerThreadLocal(this.sqlEnhancer);
        String statementName = this.getStatementName("query", name);
        return this.getSqlMapClientTemplate().queryForList(statementName, parameters, 0, -999999);
    }

    public int getMaxQueryCount() {
        return this.maxQueryCount;
    }

    public List query(Object parameters, int offset, int limit) {
        return this.query("", parameters, offset, limit);
    }

    public List query(String name, Object parameters, DomainQuery.PageStatus pageStatus) {
        int limit = pageStatus.getPageSize();
        int offset = pageStatus.getPageNumber() * pageStatus.getPageSize();
        return this.query(name, parameters, offset, limit);
    }

    public List query(String name, Object parameters, int offset, int limit) {
        SQLEnhancerManager.setSqlEnhancerThreadLocal(this.sqlEnhancer);
        if (limit > this.maxQueryCount) {
            throw new IllegalArgumentException("Limit must be less than " + this.maxQueryCount + " " + limit);
        } else {
            if (limit < 0 && limit != -999999) {
                limit = this.maxQueryCount;
            }

            String statementName = this.getStatementName("query", name);
            if (limit == -999999) {
                if (parameters == null) {
                    parameters = new HashMap();
                }

                return this.getExtendedSqlMapClientTemplate().queryForList(statementName, parameters);
            } else if (!this.autoLimit && !StringUtils.isNotBlank(this.orderKey)) {
                return this.getSqlMapClientTemplate().queryForList(statementName, parameters, offset, limit);
            } else {
                if (parameters == null) {
                    parameters = new HashMap();
                }

                return this.getExtendedSqlMapClientTemplate().queryForList(statementName, parameters, offset, limit, this.orderKey, this.autoLimit);
            }
        }
    }

    public int count(Object parameters) {
        return this.count("", parameters);
    }

    public int count(String name, Object parameters) {
        SQLEnhancerManager.setSqlEnhancerThreadLocal(this.sqlEnhancer);
        String statementName = this.getStatementName(this.autoCount ? "query" : "count", name);
        if (this.autoCount) {
            return this.getExtendedSqlMapClientTemplate().queryForCount(statementName, parameters);
        } else {
            Object count = this.getSqlMapClientTemplate().queryForObject(statementName, parameters);
            return (Integer)count;
        }
    }

    public Object get(String property, Serializable value) {
        return this.get("", property, value);
    }

    public Object get(String name, String property, Serializable value) {
        Map parameters = new HashMap(1);
        parameters.put(property, value);
        List list = this.query(name, parameters, 0, 1);
        return list.isEmpty() ? null : list.get(0);
    }

    public Object load(String property, Serializable value) {
        return this.load("", property, value);
    }

    public Object load(String name, String property, Serializable value) {
        Object object = this.get(name, property, value);
        if (object == null) {
            throw new PlatException("E_Plat_ObjectNotFound", new Object[]{property, value});
        } else {
            return object;
        }
    }

    public void insert(Object object) {
        this.insert("", object);
    }

    public void insert(String name, Object object) {
        SQLEnhancerManager.setSqlEnhancerThreadLocal(this.sqlEnhancer);
        String statementName = this.getStatementName("insert", name);
        if (DaoSecurityManager.isValidationEnabled()) {
            boolean ok = DaoSecurityManager.isPermitted(statementName, object);
            if (!ok) {
                throw new AccessDeniedException("Permission Denied in INSERT operation");
            }
        }

        Iterator var6;
        if (this.insertMonitors != null && !this.insertMonitors.isEmpty()) {
            var6 = this.insertMonitors.iterator();

            while(var6.hasNext()) {
                SqlMapInsertMonitor monitor = (SqlMapInsertMonitor)var6.next();
                monitor.beforeInsert(statementName, object);
            }
        }

        this.getSqlMapClientTemplate().insert(statementName, object);
        if (this.insertListeners != null && !this.insertListeners.isEmpty()) {
            var6 = this.insertListeners.iterator();

            while(var6.hasNext()) {
                SqlMapInsertListener listener = (SqlMapInsertListener)var6.next();
                listener.afterInsert(statementName, object);
            }
        }

    }

    public int update(Object object) {
        return this.update("", object);
    }

    public int update(String name, Object object) {
        SQLEnhancerManager.setSqlEnhancerThreadLocal(this.sqlEnhancer);
        String statementName = this.getStatementName("update", name);
        if (DaoSecurityManager.isValidationEnabled()) {
            boolean ok = DaoSecurityManager.isPermitted(statementName, object);
            if (!ok) {
                throw new AccessDeniedException("Permission Denied in UPDATE operation ");
            }
        }

        if (this.updateMonitors != null && !this.updateMonitors.isEmpty()) {
            Iterator var7 = this.updateMonitors.iterator();

            while(var7.hasNext()) {
                SqlMapUpdateMonitor monitor = (SqlMapUpdateMonitor)var7.next();
                monitor.beforeUpdate(statementName, object);
            }
        }

        int result = this.getSqlMapClientTemplate().update(statementName, object);
        if (this.updateListeners != null && !this.updateListeners.isEmpty()) {
            Iterator var9 = this.updateListeners.iterator();

            while(var9.hasNext()) {
                SqlMapUpdateListener listener = (SqlMapUpdateListener)var9.next();
                listener.afterUpdate(statementName, object);
            }
        }

        return result;
    }

    public int delete(Object parameters) {
        return this.delete("", parameters);
    }

    public int delete(String name, Object parameters) {
        SQLEnhancerManager.setSqlEnhancerThreadLocal(this.sqlEnhancer);
        String statementName = this.getStatementName("delete", name);
        if (DaoSecurityManager.isValidationEnabled()) {
            boolean ok = DaoSecurityManager.isPermitted(statementName, parameters);
            if (!ok) {
                throw new AccessDeniedException("Permission Denied in DELETE operation");
            }
        }

        if (this.deleteMonitors != null && !this.deleteMonitors.isEmpty()) {
            Iterator var7 = this.deleteMonitors.iterator();

            while(var7.hasNext()) {
                SqlMapDeleteMonitor monitor = (SqlMapDeleteMonitor)var7.next();
                monitor.beforeDelete(statementName, parameters);
            }
        }

        int result = this.getSqlMapClientTemplate().delete(statementName, parameters);
        if (this.deleteListeners != null && !this.deleteListeners.isEmpty()) {
            Iterator var9 = this.deleteListeners.iterator();

            while(var9.hasNext()) {
                SqlMapDeleteListener listener = (SqlMapDeleteListener)var9.next();
                listener.afterDelete(statementName, parameters);
            }
        }

        return result;
    }

    public Object getNextPK() {
        SQLEnhancerManager.setSqlEnhancerThreadLocal(this.sqlEnhancer);
        return this.getSqlMapClientTemplate().queryForObject(this.name + ".nextpk", (Object)null);
    }

    public void setBeanName(String name) {
        this.name = name;
    }

    protected String getStatementName(String operation, String name) {
        return name.indexOf(".") > -1 ? name : this.name + "." + operation + StringUtils.capitalize(name);
    }

    public void setAutoLimit(boolean autoLimit) {
        this.autoLimit = autoLimit;
    }

    public void setAutoCount(boolean autoCount) {
        this.autoCount = autoCount;
    }

    protected SqlMapClientTemplate getExtendedSqlMapClientTemplate() {
        if (!(this.getSqlMapClient() instanceof SqlMapClient)) {
            throw new IllegalStateException("SqlMapClient should be instance of " + SqlMapClient.class.getName() + " while use auto count or limit.\r\nNote: You can use " + SqlMapClientFactoryBean.class.getName() + " to retrieve a proper SqlMapClient implementation");
        } else if (!(this.getSqlMapClientTemplate() instanceof SqlMapClientTemplate)) {
            throw new IllegalStateException("SqlMapClientTemplate should be instance of " + SqlMapClientTemplate.class.getName() + " while use auto count or limit.");
        } else {
            return (SqlMapClientTemplate)this.getSqlMapClientTemplate();
        }
    }

    public void setOrderKey(String orderBy) {
        this.orderKey = orderBy;
    }

    public int deleteBatch(String name, Collection parameters) {
        SQLEnhancerManager.setSqlEnhancerThreadLocal(this.sqlEnhancer);
        String statementName = this.getStatementName("delete", name);
        if (DaoSecurityManager.isValidationEnabled()) {
            boolean ok = DaoSecurityManager.isPermitted(statementName, parameters);
            if (!ok) {
                throw new AccessDeniedException("Permission Denied in DELETE operation");
            }
        }

        if (this.deleteMonitors != null && !this.deleteMonitors.isEmpty()) {
            Iterator var7 = this.deleteMonitors.iterator();

            while(var7.hasNext()) {
                SqlMapDeleteMonitor monitor = (SqlMapDeleteMonitor)var7.next();
                monitor.beforeDelete(statementName, parameters);
            }
        }

        int result = ((SqlMapClientTemplate)this.getSqlMapClientTemplate()).deleteBatch(statementName, parameters);
        if (this.deleteListeners != null && !this.deleteListeners.isEmpty()) {
            Iterator var9 = this.deleteListeners.iterator();

            while(var9.hasNext()) {
                SqlMapDeleteListener listener = (SqlMapDeleteListener)var9.next();
                listener.afterDelete(statementName, parameters);
            }
        }

        return result;
    }

    public int updateBatch(String name, Collection parameters) {
        SQLEnhancerManager.setSqlEnhancerThreadLocal(this.sqlEnhancer);
        String statementName = this.getStatementName("update", name);
        if (DaoSecurityManager.isValidationEnabled()) {
            boolean ok = DaoSecurityManager.isPermitted(statementName, parameters);
            if (!ok) {
                throw new AccessDeniedException("Permission Denied in UPDATE operation ");
            }
        }

        if (this.updateMonitors != null && !this.updateMonitors.isEmpty()) {
            Iterator var7 = this.updateMonitors.iterator();

            while(var7.hasNext()) {
                SqlMapUpdateMonitor monitor = (SqlMapUpdateMonitor)var7.next();
                monitor.beforeUpdate(statementName, parameters);
            }
        }

        int result = ((SqlMapClientTemplate)this.getSqlMapClientTemplate()).updateBatch(statementName, parameters);
        if (this.updateListeners != null && !this.updateListeners.isEmpty()) {
            Iterator var9 = this.updateListeners.iterator();

            while(var9.hasNext()) {
                SqlMapUpdateListener listener = (SqlMapUpdateListener)var9.next();
                listener.afterUpdate(statementName, parameters);
            }
        }

        return result;
    }

    public int insertBatch(String name, Collection parameters) {
        SQLEnhancerManager.setSqlEnhancerThreadLocal(this.sqlEnhancer);
        String statementName = this.getStatementName("insert", name);
        if (DaoSecurityManager.isValidationEnabled()) {
            boolean ok = DaoSecurityManager.isPermitted(statementName, parameters);
            if (!ok) {
                throw new AccessDeniedException("Permission Denied in INSERT operation");
            }
        }

        if (this.insertMonitors != null && !this.insertMonitors.isEmpty()) {
            Iterator var7 = this.insertMonitors.iterator();

            while(var7.hasNext()) {
                SqlMapInsertMonitor monitor = (SqlMapInsertMonitor)var7.next();
                monitor.beforeInsert(statementName, parameters);
            }
        }

        int result = ((SqlMapClientTemplate)this.getSqlMapClientTemplate()).insertBatch(statementName, parameters);
        if (this.insertListeners != null && !this.insertListeners.isEmpty()) {
            Iterator var9 = this.insertListeners.iterator();

            while(var9.hasNext()) {
                SqlMapInsertListener listener = (SqlMapInsertListener)var9.next();
                listener.afterInsert(statementName, parameters);
            }
        }

        return result;
    }
}
